﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.SessionState;

namespace RDIFramework.WebApp.Modules.handler
{
    using RDIFramework.Utilities;
    using RDIFramework.BusinessEntity;
    using RDIFramework.ServiceAdapter;
    using RDIFramework.WebCommon;

    /// <summary>
    /// DBLinkAdminHandler 的摘要说明
    /// </summary>
    public class DBLinkAdminHandler : IHttpHandler, IRequiresSessionState
    {
        private string action
        {
            get
            {
                return RDIFramework.WebCommon.PublicMethod.GetString(getObj("action"));
            }
        }
        private object getObj(string key) { return RDIFramework.WebCommon.StringHelper.GetRequestObject(key); }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            switch(action)
            {
                case "GetList":
                    GetList(context);
                    break;
                case "GetEntity":
                    GetEntity(context);
                    break;
                case "AddDBLink":
                    AddDBLink(context);
                    break;
                case "EditDBLink":
                    EditDBLink(context);
                    break;
                case "DeleteDBLink":
                    DeleteDBLink(context);
                    break;
            }
        }

        private void GetList(HttpContext ctx)
        {
            string json = "[]";
            DataTable dtDbLink = RDIFramework.ServiceAdapter.RDIFrameworkService.Instance.DbLinkDefineService.GetDT(Utils.UserInfo);
            json = RDIFramework.WebCommon.JSONhelper.ToJson(dtDbLink);
            ctx.Response.Write(json);
        }

        private void GetEntity(HttpContext ctx)
        {
            CiDbLinkDefineEntity entity = RDIFramework.ServiceAdapter.RDIFrameworkService.Instance.DbLinkDefineService.GetEntity(Utils.UserInfo, PublicMethod.GetString(getObj("KeyId")));
            if (!string.IsNullOrEmpty(entity.LinkData))
            {
                entity.LinkData = SecretHelper.AESDecrypt(entity.LinkData);
            }
            ctx.Response.Write(RDIFramework.WebCommon.JSONhelper.ToJson(entity));
        }

        private CiDbLinkDefineEntity GetPageEntity(CiDbLinkDefineEntity entity)
        {
            entity.LinkName = PublicMethod.GetString(getObj("LinkName"));
            entity.LinkData = PublicMethod.GetString(getObj("LinkData"));
            if (!string.IsNullOrEmpty(entity.LinkData))
            {
                entity.LinkData = SecretHelper.AESEncrypt(entity.LinkData);
            }
            entity.LinkType = PublicMethod.GetString(getObj("vlinktype"));
            entity.Description = PublicMethod.GetString(getObj("Description"));
            entity.Enabled = PublicMethod.GetString(getObj("Enabled")) == "on" ? 1 : 0;
            return entity;
        }


        private void AddDBLink(HttpContext ctx)
        {
            CiDbLinkDefineEntity entity = new CiDbLinkDefineEntity();
            UserInfo vUser = Utils.UserInfo;
            entity = this.GetPageEntity(entity);
            entity.DeleteMark = 0;
            if (vUser != null)
            {
                entity.CreateBy = vUser.RealName;
                entity.CreateUserId = MathHelper.ToInteger(vUser.Id);
            }
            string statusMessage = string.Empty;
            string statusCode = string.Empty;
            int successFlag = 0;
            try
            {
                RDIFrameworkService.Instance.DbLinkDefineService.Add(vUser, entity, out statusCode, out statusMessage);
                if (statusCode == StatusCode.OKAdd.ToString())
                {
                    successFlag = 1;
                    ctx.Response.Write(new JsonMessage { Success = true, Data = successFlag.ToString(), Message = statusMessage }.ToString());
                }
                else
                {
                    ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = statusMessage }.ToString());
                }
            }
            catch (Exception ex)
            {
                ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = "添加数据失败！" + ex.Message }.ToString());
            }
        }

        private void EditDBLink(HttpContext ctx)
        {
            UserInfo vUser = Utils.UserInfo;
            string vId = PublicMethod.GetString(getObj("KeyId"));
            CiDbLinkDefineEntity entity = RDIFrameworkService.Instance.DbLinkDefineService.GetEntity(vUser, vId);
            entity = this.GetPageEntity(entity);
            if (vUser != null)
            {
                entity.ModifiedBy = vUser.RealName;
                entity.ModifyUserId = MathHelper.ToInteger(vUser.Id);
            }
            string statusMessage = string.Empty;
            string statusCode = string.Empty;
            int successFlag = 0;
            try
            {
                RDIFrameworkService.Instance.DbLinkDefineService.Update(vUser, entity, out statusCode, out statusMessage);

                if (statusCode == StatusCode.OKUpdate.ToString())
                {
                    successFlag = 1;
                    ctx.Response.Write(new JsonMessage { Success = true, Data = successFlag.ToString(), Message = statusMessage }.ToString());
                }
                else
                {
                    ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = statusMessage }.ToString());
                }
            }
            catch (Exception ex)
            {
                ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = "更新数据失败！" + ex.Message }.ToString());
            }     
        }

        private void DeleteDBLink(HttpContext ctx)
        {
            string vId = PublicMethod.GetString(getObj("KeyId"));
            UserInfo vUser = Utils.UserInfo;
            int successFlag = 0;

            try
            {
                int returnValue = RDIFrameworkService.Instance.DbLinkDefineService.SetDeleted(Utils.UserInfo, new string[] { vId });

                if (returnValue > 0)
                {
                    successFlag = 1;
                    ctx.Response.Write(new JsonMessage { Success = true, Data = successFlag.ToString(), Message = RDIFrameworkMessage.MSG0013 }.ToString());
                }
                else
                {
                    ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = RDIFrameworkMessage.MSG3020 }.ToString());
                }
            }
            catch (Exception ex)
            {
                ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = RDIFrameworkMessage.MSG3020 + ex.Message }.ToString());
            } 
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}